﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace MyGame.Objects
{
    public class Bandit
    {
        private int positionX;
        private int positionY;
        private int moveX = 0;
        private int moveY = 0;

        //Atribute positionX
        public int PositionX
        {
            get { return this.positionX; }
            set { this.positionX = value; }
        }

        //Atribute positionY
        public int PositionY
        {
            get { return this.positionY; }
            set { this.positionY = value; }
        }

        // * Methods for moving on gamefield, 
        //   called when keyboard arrows are pressed
        public void moveRight()
        {
            this.moveX = 1;
            this.moveY = 0;
            //updatePosition(this.moveX, this.moveY);
        }
        public void moveLeft()
        {
            this.moveX = -1;
            this.moveY = 0;
            //updatePosition(this.moveX, this.moveY);
        }
        public void moveUp()
        {
            this.moveX = 0;
            this.moveY = -1;
            //updatePosition(this.moveX, this.moveY);
        }

        public void moveDown()
        {
            this.moveX = 0;
            this.moveY = 1;
            //updatePosition(this.moveX, this.moveY);
        }
        
        // Updates position of bandit
        public void updatePosition()
        {
                this.positionX += this.moveX;
                this.positionY += this.moveY;    
        }
        
        //Collision Checking
        public bool CheckForCollision(Level actualLevel )
        {
            bool collision = false;
            
            //Checking for collisions with borders of canvas (in case someone forget make wall)
            if ((this.positionX + this.moveX) < 0)
            {
                this.moveX = 0;   /* left border */
                return collision;
            }
            if ((this.positionY + this.moveY) < 0)
            {
                this.moveY = 0;   /* top border */
                return collision;
            }
            if ((this.positionX + this.moveX) > Settings.gameFieldWidth)
            {
                //this.positionX = Settings.gameFieldWidth;    /* bottom border */
                this.moveX = 0; /* left border */
                return collision;
            }
            if ((this.positionY + this.moveY) > Settings.gameFieldHeight)
            {
                //this.positionY = Settings.gameFieldHeight;   /* right border */
                this.moveY = 0;   /* top border */
                return collision;
            }

            Cell nextCell =
                actualLevel.cellList.Where(x => x.PositionX == (this.positionX + this.moveX)).Where(y => y.PositionY == (this.positionY + this.moveY)).FirstOrDefault();//(this.positionX + this.moveX)).FirstOrDefault();

            //Checking collisions with wall
            if (nextCell.CellType == '#')
            {
                collision = true;
            }

            return collision;

        }
    }
}
